var _header_service = require('../../../service/header-service')
var _account_service = require('../../../service/account-service')
var _cart_service = require('../../../service/cart-service')
const vue = require('../vue');
const _util = require('../../../util/util');
let _search_content = new vue({
    el: '#header',
    data() {
        return {
            keyword: '',
            categories: [],
            selectedItem: '全部类别',
            pageid: 1,
            pagesize: 6,
            sortby: 0,
            username: "",
            password: "",
            pwd: "",
            role: "",
            phone: '',
            code: '',
            cartItemsNum: 0,
        }
    },
    methods: {
        getItems: function () {
            return this.categories;
        },
        send: function (event) {
            urlStr = '/view/products.html?pagesize=6&pageid=1&sortby=0&keyword=' + this.keyword + '&categoryid=' + this.switchid();
            window.location.href = urlStr;
            window.event.returnValue = false;
        },
        switchid: function () {
            for (let i = 0; i < this.categories.length; i++) {
                if (this.selectedItem === this.categories[i].name)
                    return i + 1;
            }
            return 0;
        },
        doLogin: function () {
            var _this = this;
            if (this.role === "customer") {
                _account_service.usersLogin(
                    {
                        username: _this.username,
                        password: _this.pwd
                    },
                    function () {
                        sessionStorage.setItem("user", JSON.stringify({
                            username: _this.username,
                            role: _this.role
                        }));
                        window.location.href = "/view/catalog-main.html";
                    },
                    function (err) {
                        console.log(err);
                    }
                )
            } else {
                _account_service.sellersLogin(
                    {
                        username: _this.username,
                        password: _this.pwd
                    },
                    function () {
                        sessionStorage.setItem("user", JSON.stringify({
                            username: _this.username,
                            role: _this.role
                        }));
                        window.location.href = "/view/catalog-main.html";
                    },
                    function (err) {
                        console.log(err);
                    }
                )
            }
        },
        doLogout: function () {
            window.location.href = "/view/catalog-main.html";
            let user = _util.getUser();
            if (user.role === "customer") {
                _account_service.usersLogout(function () {
                    sessionStorage.removeItem("user");
                }, function (err) {
                    console.log(err);
                });
            } else {
                _account_service.sellersLogout(function () {
                    sessionStorage.removeItem("user");
                }, function (err) {
                    console.log(err);
                });
            }
        },
        getUsername: function () {
            if (_util.checkLogin()) {
                return _util.getUser().username;
            } else {
                return ""
            }
        },
        checkLogin: function () {
            return _util.checkLogin();
        },
        isUser: function () {
            return _util.getUser().role === "customer";
        },
        getSMS: function () {
            _account_service.getSMS(
                this.phone,
                function (res) {
                    alert('短信已发送')
                },
                function (errMsg) {
                    console.log(errMsg)
                },
            )
        },
        loginBySMS: function () {
            let self = this;
            if (this.role == 'customer') {
                _account_service.usersLoginBySMS(
                    this.phone,
                    this.code,
                    function (res) {
                        sessionStorage.setItem('user', JSON.stringify({
                            username: self.username,
                            role: self.role
                        }));
                        window.location.href = '/view/catalog-main.html';
                    },
                    function (errMsg) {
                        console.log(errMsg);
                    }
                )
            } else {
                _account_service.sellersLoginBySMS(
                    this.phone,
                    this.code,
                    function (res) {
                        sessionStorage.setItem('user', JSON.stringify({
                            username: self.username,
                            role: self.role
                        }));
                        window.location.href = '/view/catalog-main.html';
                    },
                    function (errMsg) {
                        console.log(errMsg);
                    }
                )
            }
        },
        resetPwd: function () {
            let self = this;
            if (this.role == 'customer') {
                _account_service.usersResetPwd(
                    this.phone,
                    this.password,
                    this.code,
                    function (res) {
                        alert("密码修改成功！")
                        sessionStorage.setItem('user', JSON.stringify({
                            username: self.username,
                            role: self.role
                        }));
                        window.location.href = '/view/catalog-main.html';
                    },
                    function (errMsg) {
                        alert(errMsg);
                    }
                )
            } else {
                _account_service.sellersResetPwd(
                    this.phone,
                    this.password,
                    this.code,
                    function (res) {
                        sessionStorage.setItem('user', JSON.stringify({
                            username: self.username,
                            role: self.role
                        }));
                        window.location.href = '/view/catalog-main.html';
                    },
                    function (errMsg) {
                        alert(errMsg);
                    }
                )
            }
        },
        getCartNums: function () {
            let self = this;
            if (_util.checkLogin()) {
                _cart_service.getCartNums(
                    function (res) {
                        self.cartItemsNum = res;
                    },
                    function (errMsg) {
                        console.log(errMsg)
                    }
                )
            }
            return this.cartItemsNum
        },
    }
});


var _header = {
    _this: this,
    init: function () {
        this.bindEvents();
        this.loadSortInfo();
    },
    bindEvents: function () {

    },
    loadSortInfo: function () {
        _header_service.getAllCategories(
            function (res) {
                _search_content.categories = res;
                console.log(_search_content.selectedItem)
            },
            function (errMsg) {
                console.log("failed!")
            },
        )
    },
}

module.exports = _header.init();